﻿using AutoMapper;
using MediatR;
using Newtonsoft.Json;
using T.S.BaseData.Api.AppClinet.Commend.Goods;
using T.S.BaseData.Api.Dto.Goods;
using T.S.BaseData.Api.Exit;
using T.S.BaseData.Domain.Entity;
using T.S.BaseData.ErrorCode;
using T.S.BaseData.Infrastructure.Base;

namespace T.S.BaseData.Api.AppClinet.CommendHand.Goods
{
    public class BasicinfoShowCommandHandle : IRequestHandler<BasicinfoShowCommand, ApiPaging<ShowBasicinfoDto>>
    {
        private readonly IBaseRepository<GoodsInfo> goodsRepo;
        private readonly IBaseRepository<BasicInfo> basicRepo;
        private readonly IMapper mapper;

        public BasicinfoShowCommandHandle(IBaseRepository<GoodsInfo> goodsRepo,
            IBaseRepository<BasicInfo> basicRepo,
            IMapper mapper
            )
        {
            this.goodsRepo = goodsRepo;
            this.basicRepo = basicRepo;
            this.mapper = mapper;
        }

        public async Task<ApiPaging<ShowBasicinfoDto>> Handle(BasicinfoShowCommand request, CancellationToken cancellationToken)
        {

            var good = goodsRepo.GetAll();
            var basic = basicRepo.GetAll();

            ApiPaging<ShowBasicinfoDto> result = new ApiPaging<ShowBasicinfoDto>();

            //查询条件

            //Redis 
            //var csRedis = new CSRedis.CSRedisClient("127.0.0.1:6379,dafaultDatabase=1,prefix=_My");
            //var cachedData = await csRedis.GetAsync("Basic");

            //if (!string.IsNullOrEmpty(cachedData))
            //{
            //    result.Iteims = ExitHetp.TolistData<List<ShowBasicinfoDto>>(cachedData);  //JsonConvert.DeserializeObject<List<ShowBasicinfoDto>>(cachedData); 
            //    return result;
            //}

            var list = from g in good
                       join b in basic on g.Id equals b.GoodsId
                       select new ShowBasicinfoDto {
                       Id = b.Id,
                       GoodsId = g.Id,
                       GoodsName = g.GoodsName,
                       GoodsBatch = b.GoodsBatch,
                       GoodsEnterprise = b.GoodsEnterprise,
                       GoodsExpiration = b.GoodsExpiration,
                       GoodsLicense = b.GoodsLicense,
                       GoodsPhoto = b.GoodsPhoto,
                       CreatePeop = b.CreatePeop,
                       CreateTime = b.CreateTime,
                       IsDelete = b.IsDelete
                       };
            result.TotalCount = list.Count();
            result.TotalPage =(int) Math.Ceiling(result.TotalCount * 1.0) / request.PageSize;
            var model = mapper.Map<List<ShowBasicinfoDto>>(list);
            result.Iteims = model.OrderBy(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            result.Code = ApiEnums.Success;
            result.Meg = "显示商品基本信息成功";

            //await csRedis.SetAsync(
            //    "Basic",
            //    ExitHetp.Tostirng(result.Iteims)
            //    //JsonConvert.SerializeObject(result.Iteims)  
            //    , 60000
            //    );


            return result;
        }
    }
}
